package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.StringUtil;
import com.wyh.entity.*;
import com.wyh.mapper.*;
import com.wyh.request.MsgRecontactRequest;
import com.wyh.request.MsgVerifyMessageRequest;
import com.wyh.service.IMsgRecontactService;
import com.wyh.vo.VerifyMessageVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 联系人,非好友业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-08
 */
@Slf4j
@Service
@AllArgsConstructor
public class MsgRecontactServiceImpl implements IMsgRecontactService {

    private final MsgRecontactMapper baseMapper;

    private final AdminUserMapper adminUserMapper;

    private final AdminBlackListMapper adminBlackListMapper;

    private final MsgVerifyMessageMapper msgVerifyMessageMapper;

    private final MsgChatRoomMapper msgChatRoomMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> deleteOne(String friendId) {
        //获取当前登录者的id 模拟为666666
        String userId = "666666";

        if (StringUtils.isNotBlank(friendId)){
            return R.error("参数错误");
        }
        baseMapper.delete(Wrappers.lambdaQuery(MsgRecontact.class).eq(MsgRecontact::getUserId, userId).eq(MsgRecontact::getFriendId, friendId));
        return R.success("删除成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<AdminUser> queryById(String userId) {


        if (StringUtil.isEmpty(userId)){
            return R.error("参数错误");
        }

        AdminUser adminUser = adminUserMapper.selectOne(Wrappers.lambdaQuery(AdminUser.class).eq(AdminUser::getUserId, userId));


        return R.success(adminUser);
    }


    @Override
    public R<String> addFriend(MsgRecontactRequest msgRecontactRequest) {

        //添加好友逻辑
        return null;
    }

    @Override
    public Boolean isOutFriendMax(String userId) {
        Long aLong = baseMapper.selectCount(Wrappers.lambdaQuery(MsgRecontact.class).eq(MsgRecontact::getUserId, userId));
        if (aLong==3000){
            return true;
        }
        return false;
    }

    @Override
    public Boolean isFriend(String userId1, String userId2) {
        MsgRecontact msgRecontact = baseMapper.selectOne(Wrappers.lambdaQuery(MsgRecontact.class)
                .eq(MsgRecontact::getUserId, userId1)
                .eq(MsgRecontact::getFriendId, userId2));
        if (msgRecontact != null) {
            return true;
        }
        return false;
    }

    @Override
    public R<List<MsgRecontact>> queryList() {
        //获取 当前登录者的id 模拟为666666
        String userId = "666666";
        //排除黑名单用户
        List<MsgRecontact> msgRecontacts = baseMapper.
                selectList(Wrappers.lambdaQuery(MsgRecontact.class)
                        .eq(MsgRecontact::getUserId, userId)
                        .eq(MsgRecontact::getIsBlack,0));

        return R.success(msgRecontacts);
    }

    @Override
    public R<String> updateRemark(String friendId, String remark) {


        //模拟当前用户位66666
        String userId = "666666";

        if (StringUtils.isNotBlank(friendId)){
            return R.error("参数错误");
        }
        MsgRecontact build = MsgRecontact.builder().remark(remark).build();

        baseMapper.update(build,Wrappers.lambdaQuery(MsgRecontact.class)
                .eq(MsgRecontact::getUserId, userId).eq(MsgRecontact::getFriendId, friendId));


        return R.success("备注修改成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MsgRecontact add(MsgRecontact request) {
        baseMapper.insert(request);
        return request;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(MsgRecontactRequest request) {
        MsgRecontact entity = MsgRecontact.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public MsgRecontact getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<MsgRecontact> pageList(MsgRecontactRequest request) {
        Page<MsgRecontact> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<MsgRecontact> query = this.buildWrapper(request);
        IPage<MsgRecontact> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<MsgRecontact> list(MsgRecontactRequest request) {
        LambdaQueryWrapper<MsgRecontact> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<MsgRecontact> buildWrapper(MsgRecontactRequest param) {
        LambdaQueryWrapper<MsgRecontact> query = new LambdaQueryWrapper<>();

         if (StringUtils.isNotBlank(param.getNickName())){
               query.like(MsgRecontact::getNickName ,param.getNickName());
        }
         if (StringUtils.isNotBlank(param.getAvatar())){
               query.like(MsgRecontact::getAvatar ,param.getAvatar());
        }

        if (param.getCreateUserId() != null){
               query.eq(MsgRecontact::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(MsgRecontact::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
